package timingwheel

import "time"

type Task struct {
	Delay    time.Duration // 延迟执行时间，单位：毫秒
	cancel   bool
	schedule *Schedule
	iJob     IJob
}

func NewTask(job IJob, schedule *Schedule) *Task {
	task := &Task{
		cancel:   false,
		schedule: schedule,
		iJob:     job,
		Delay:    job.Delay(),
	}
	job.Init(task)
	return task
}

func (t *Task) Cancel() {
	t.cancel = true
}

func (t *Task) IsCancel() bool {
	return t.cancel
}

func (t *Task) Run() {
	t.iJob.Run(t)
	if t.iJob.IsCron() && !t.IsCancel() {
		// 定时任务,重新投放
		t.schedule.PushJob(t.iJob)
	}
}

func (t *Task) String() string {
	return t.iJob.String()
}
